package com.sk.weichat.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.sk.weichat.R;

/**
 * @author lzs
 * @date 2019/10/22
 * Email:343067508@qq.com
 **/
public class VoiceView extends FrameLayout {
    ProgressBar progressBar;
    TextView tvTime;

    AnimationDrawable animationDrawable;

    public VoiceView(Context context) {
        this(context, null);
    }

    public VoiceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoiceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_voice_left, null);
        progressBar = view.findViewById(R.id.progressBar);

        animationDrawable = (AnimationDrawable) view.findViewById(R.id.iv_voice_anim).getBackground();

        tvTime = view.findViewById(R.id.tv_time);
        addView(view);
    }

    public void startAnim() {
        if (animationDrawable != null) {
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
                animationDrawable.selectDrawable(0);
            }
            animationDrawable.start();
        }
    }

    public void stopAnim() {
        if (animationDrawable != null) {
            animationDrawable.stop();
            animationDrawable.selectDrawable(0);
        }
    }

    public void setMaxProgress(int maxProgress) {
        if (progressBar != null) {
            progressBar.setMax(maxProgress);
        }
    }

    public void updateProgress(int pro) {
        if (progressBar == null) {
            return;
        }
        progressBar.setProgress(pro);
    }

    public void setProgressBarVisibility(int visibility) {
        if (progressBar != null) {
            progressBar.setVisibility(visibility);
        }
    }


    public void setTime(String time) {
        tvTime.setText(time);
    }
}
